$('.typeahead').typeahead({
	property: "name",
    source: function (typeahead, query) {
    	var options = {page : 1, max : 5, searchText : typeahead.query};
        return $.get('/film/util/getSearchable.do', options, function (data) {
        	if (data && data.data) {
        		var names = new Array();
        		$.each(data.data, function(key, cur) {
        			if (cur.projectName) {
        				names.push({name : cur.projectName, type : "project" , id : cur.id});
        			} else if (cur.name && cur.surname) {
        				names.push({name : cur.name + " " + cur.surname, type : "user", id : cur.id})
        			}
        		});
        		
        		return typeahead.process(names);
        	}
        });
    },
	onselect: function (obj) {
		if (obj.type == "user")	{
			window.location.href = "/film/user/getUser.do?userId=" + obj.id;
		} else if (obj.type == "project") {
			window.location.href = "/film/project/getProject.do?pid=" + obj.id;
		}
    }
});

$("#logout").click(function() {
	$.ajax("/film/user/logout.do").done(function() {
		window.location.href = "/film";
	});
});